//
//  SupplyAndDemandVC.m
//  phmall
//
//  Created by admin on 2017/11/3.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "SupplyAndDemandVC.h"
#import "SDCursorView.h"
#import "GroupBuyingListVC.h"
#import "SupplyAndDemandListVC.h"
#import "ClassifyView.h"
#import "SupplyAndDemandSearchVC.h"
#import "SupplyAndDemandDetailsVC.h"
#import "FUIImageView.h"
@interface SupplyAndDemandVC ()<ClassifyViewDelegate,SDCursorViewDelegate,SupplyAndDemandListVCDelegate >{
    ClassifyView    *classView;
    UIButton        *more;
    FUIImageView    *_headView;
    UIView          *_flage;
    NSString        *type;
}
@property(nonatomic, strong)SDCursorView *cursorView;
@property(nonatomic, strong)NSMutableArray *titleArray;
@property(nonatomic, strong)NSMutableArray *contrors;
@end

@implementation SupplyAndDemandVC
-(void)didSelectRowAtIndexPath:(NSString *)sender{
    self.hidesBottomBarWhenPushed=YES;
    SupplyAndDemandDetailsVC *sear=[[SupplyAndDemandDetailsVC alloc] init];
    sear.uid=sender;
    sear.type=type;
    [self.navigationController pushViewController:sear animated:YES];
    self.hidesBottomBarWhenPushed=NO;
}
-(void)leftBarAction{
    self.hidesBottomBarWhenPushed=YES;
    SupplyAndDemandSearchVC *sear=[[SupplyAndDemandSearchVC alloc] init];
    [self.navigationController pushViewController:sear animated:YES];
    self.hidesBottomBarWhenPushed=NO;
//    [self.navigationController popViewControllerAnimated:YES];
}
-(void)rightBarAction{
//    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navBarBgAlpha=@"1.0";
    // 设置子类的NavBar上的渲染颜色
    self.navigationController.navigationBar.tintColor = [UIColor clearColor];
    // 改变NavBar底层View的颜色
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    // 改变NavBar顶层View的颜色
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self getNavBarView];
    type=@"suppy";
    self.index=0;
    UIBarButtonItem* rightBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"消息"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction)];
 
    self.navigationItem.rightBarButtonItem=rightBar;
    
    UIBarButtonItem* leftBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"搜索白色"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem=leftBar;
 
    YYCache *cache = [YYCache cacheWithName:@"category"];
    if ([cache containsObjectForKey:@"name"]) {
        NSMutableArray *category = (NSMutableArray *)[cache objectForKey:@"name"];
        NSLog(@"%@",category);
        [self reloadPages:category];
    }
 
    // Do any additional setup after loading the view.
}

-(void)reloadPages:(NSMutableArray *)title{
    NSMutableArray *classtitle=[[NSMutableArray alloc]init];
    for (int i=0; i<title.count; i++) {
       
        NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"appLanguage"];
        if ([language hasPrefix:@"en"]) {
            
            [classtitle addObject:title[i][@"category_name"]];
        } else if ([language hasPrefix:@"zh"]) {
            
            [classtitle addObject:title[i][@"category_name_ch"]];
        }
        
        [self.titleArray addObject:title[i]];
    }
    _cursorView = [[SDCursorView alloc]init];
    [self.view addSubview:_cursorView];
    [_cursorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(44);
    }];
    [_cursorView.superview layoutIfNeeded];
    //设置子页面容器的高度
    _cursorView.contentViewHeight = SCREEN_HEIGHT-44-(STATUS_BAR_HEIGHT+44)-self.tabBar_height-49;
    _cursorView.delegate=self;
    //设置控件所在controller
    _cursorView.parentViewController = self;
    _cursorView.titles =classtitle;
    //    _cursorView.titles=titles;
    //设置所有子controller
    self.contrors = [NSMutableArray array];
    _cursorView.currentIndex=self.index;
    
    for (int i=0; i<classtitle.count; i++) {
        SupplyAndDemandListVC * controller = [[SupplyAndDemandListVC alloc]init];
        controller.uid=title[i][@"uid"];
        controller.delegate=self;
        controller.type=type;
        [self.contrors  addObject:controller];
    }
    _cursorView.controllers = [self.contrors copy];
    //设置字体和颜色
    _cursorView.normalColor = [UIColor blackColor];
    _cursorView.selectedColor = [UIColor colorWithHexString:@"#ff8000"];
    _cursorView.selectedFont = [UIFont systemFontOfSize:13];
    _cursorView.normalFont = [UIFont systemFontOfSize:13];
    //    _cursorView.lineView.backgroundColor = [UIColor redColor];
    //属性设置完成后，调用此方法绘制界面
    [_cursorView reloadPages];
    
    more=[UIButton buttonWithType:UIButtonTypeCustom];
    [more setImage:[UIImage imageNamed:@"下拉展开"] forState:UIControlStateNormal];
    [more setImage:[UIImage imageNamed:@"上拉收起"] forState:UIControlStateSelected];
    more.backgroundColor = [UIColor colorWithHexString:@"#f8fafd"];
    [more addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:more];
    [more mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_cursorView).offset(0);
        make.centerY.equalTo(_cursorView);
        make.width.height.offset(44);
    }];

    classView=[[ClassifyView alloc]init];
    classView.delegate=self;
    classView.hidden=YES;
    classView.titleArray=classtitle;
    [classView setSelectAtIndex:self.index];
    [self.view addSubview:classView];
    classView.layer.masksToBounds=YES;
    [classView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_cursorView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
    
   
}
-(void)getNavBarView{
    _headView = [[FUIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-120, 44)];
    _headView.contentMode= UIViewContentModeScaleAspectFit;
    _headView.userInteractionEnabled=YES;
    _headView.backgroundColor=[UIColor clearColor];
    self.navigationItem.titleView = _headView; //设置导航栏的titleView为imageView

    UIButton *supply=[UIButton buttonWithType:UIButtonTypeCustom];
    [supply setTitle:Localized(@"供应") forState:UIControlStateNormal];
    supply.titleLabel.font=[UIFont systemFontOfSize:18];
    [supply setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [supply addTarget:self action:@selector(suppyAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:supply];
    [supply mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(70).priorityHigh();
        make.centerY.offset(0).priorityHigh();
        make.centerX.offset(-40).priorityHigh();
    }];
    
    UIButton *buy=[UIButton buttonWithType:UIButtonTypeCustom];
    [buy setTitle:Localized(@"求购") forState:UIControlStateNormal];
    buy.titleLabel.font=[UIFont systemFontOfSize:18];
    [buy addTarget:self action:@selector(buyAction:) forControlEvents:UIControlEventTouchUpInside];
    [buy setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_headView   addSubview:buy];
    [buy mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(70).priorityHigh();
        make.centerY.offset(0).priorityHigh();
        make.centerX.offset(40).priorityHigh();
    }];
    
    _flage=[[UIView alloc]init];
    _flage.backgroundColor=[UIColor whiteColor];
    [_headView addSubview:_flage];
    [_flage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(40);
        make.centerX.equalTo(supply);
        make.bottom.equalTo(supply);
        make.height.offset(3);
    }];
}
-(void)buyAction:(UIButton *)sender{
    type=@"buy";
    [UIView animateWithDuration:0.2 animations:^{
        [_flage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(40);
            make.centerX.equalTo(sender);
            make.bottom.equalTo(sender);
            make.height.offset(3);
        }];
        [_flage.superview layoutIfNeeded];
    }];
    
    _cursorView.currentIndex=self.index;
    for (SupplyAndDemandListVC *controller in self.contrors) {
        controller.type=type;
        [controller.myTableview.mj_header beginRefreshing];
    }
    _cursorView.controllers = [self.contrors copy];
    //属性设置完成后，调用此方法绘制界面
    [_cursorView reloadPages];
}
-(void)suppyAction:(UIButton *)sender{
    type=@"suppy";
    [UIView animateWithDuration:0.2 animations:^{
        [_flage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(40);
            make.centerX.equalTo(sender);
            make.bottom.equalTo(sender);
            make.height.offset(3);
        }];
        [_flage.superview layoutIfNeeded];
    }];

    _cursorView.currentIndex=self.index;
    for (SupplyAndDemandListVC *controller in self.contrors) {
        controller.type=type;
        [controller.myTableview.mj_header beginRefreshing];
    }
    _cursorView.controllers = [self.contrors copy];
    //属性设置完成后，调用此方法绘制界面
    [_cursorView reloadPages];
}
- (void)moreAction:(UIButton *)sender{
    more.selected=!more.selected;
    if (more.selected) {
        classView.hidden=NO;
    }else{
        classView.hidden=YES;
    }
}

-(void)withdrawClassifyView:(NSInteger)index{
    _cursorView.currentIndex=index;
    more.selected=!more.selected;
    classView.hidden=YES;
    //属性设置完成后，调用此方法绘制界面
    [_cursorView reloadPages];
}
-(void)withdrawClassifyView{
    more.selected=!more.selected;
    classView.hidden=YES;
}
-(void)setSelectAtIndex:(NSInteger)index{
    more.selected=NO;
    classView.hidden=YES;
    self.index=index;
    [classView setSelectAtIndex:index];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSMutableArray *)titleArray{
    if (!_titleArray) {
        _titleArray = [[NSMutableArray alloc]init];
    }
    return _titleArray;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
